import 'package:flutter/material.dart';
import 'package:lib_uikit/dimen/gt_dimensions_v3.dart';
import 'package:lib_uikit/lib_uikit.dart';

enum GTTextAreaTypeV3 { default_type, outlined }

class GTTextAreaV3 extends StatefulWidget {
  final String? title;
  final String? bottomText;
  final String? hint;
  final VoidCallback? editingCompleteCallBack;
  final ValueChanged<String>? onTextChanged;
  final GTTextAreaTypeV3? type;
  final int? maxLength;
  final FocusNode? focusNode;

  //展示Title红色星号
  final bool? showTitleMarket;

  const GTTextAreaV3({
    super.key,
    this.title,
    this.bottomText,
    this.hint,
    this.editingCompleteCallBack,
    this.onTextChanged,
    this.type,
    this.maxLength,
    this.showTitleMarket,
    this.focusNode,
  });

  @override
  State<StatefulWidget> createState() => _TextAreaStateV3();
}

class _TextAreaStateV3 extends State<GTTextAreaV3> {
  Color strokeColor = Colors.transparent;
  bool hasFocus = false;
  bool isShowDelete = false;
  late FocusNode _focusNode;
  TextEditingController _textEditingController = TextEditingController();
  GTTextAreaTypeV3 _type = GTTextAreaTypeV3.default_type;
  Color backgroundColor = GTColorsV3.neutral5.color;
  int currentLength = 0, _maxLength = 0;
  bool isError = false;

  @override
  void initState() {
    super.initState();
    _focusNode = widget.focusNode ?? FocusNode();
    _type = widget.type ?? GTTextAreaTypeV3.outlined;
    _focusNode.addListener(_onFocusChange);
  }

  void _onFocusChange() {
    setState(() {
      hasFocus = _focusNode.hasFocus;
    });
  }

  @override
  void dispose() {
    // 释放资源
    if (widget.focusNode == null) {
      _focusNode.dispose();
    }

    _textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    isShowDelete = _textEditingController.text.isNotEmpty;
    _maxLength = widget.maxLength ?? 0;
    if (_type == GTTextAreaTypeV3.outlined) {
      backgroundColor = GTColorsV3.bg1.color;
      strokeColor = GTColorsV3.divider5.color;
    }
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (widget.title != null) ...[
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                widget.title ?? "",
                style: gtTextStyleW400(fontSize: 14, color: GTColorsV3.text1.color),
              ),
              if (widget.showTitleMarket ?? false) ...[
                const SizedBox(
                  width: 4,
                ),
                Text(
                  "*",
                  style: gtTextStyleW400(fontSize: 14, color: GTColorsV3.color3.color),
                ),
              ]
            ],
          ),
          const SizedBox(
            height: 6,
          )
        ],
        Container(
            constraints: const BoxConstraints(
              minHeight: 130,
            ),
            padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 12),
            decoration: BoxDecoration(
              color: backgroundColor,
              border: Border.all(
                color: isError
                    ? GTColorsV3.funct3.color
                    : hasFocus && _type == GTTextAreaTypeV3.outlined
                        ? GTColorsV3.icon9.color
                        : strokeColor,
                width: GTDimensionsV3.strokeWidth,
              ),
              borderRadius: BorderRadius.circular(6),
            ),
            child: _child()),
        if (widget.bottomText != null) ...[
          const SizedBox(
            height: 6,
          ),
          Text(
            widget.bottomText ?? "",
            style: gtTextStyleW400(fontSize: 14, color: isError ? GTColorsV3.funct3.color : GTColorsV3.text3.color),
          ),
        ],
      ],
    );
  }

  Widget _child() {
    return Column(
      children: [
        Container(
          constraints: const BoxConstraints(
            minHeight: 84,
          ),
          child: TextField(
            textAlign: TextAlign.start,
            controller: _textEditingController,
            focusNode: _focusNode,
            cursorColor: GTColorsV3.icon5.color,
            cursorWidth: 2,
            cursorRadius: const Radius.circular(2),
            keyboardType: TextInputType.multiline,
            maxLines: null,
            maxLength: _maxLength > 0 ? _maxLength + 10 : null,
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: widget.hint,
              isCollapsed: true,
              counterText: "",
              hintStyle: gtTextStyleW500(
                fontSize: 14,
                color: hasFocus ? GTColorsV3.text4.color : GTColorsV3.text3.color,
              ),
            ),
            onChanged: (text) {
              setState(() {
                currentLength = text.length;
                if (_maxLength > 0) {
                  isError = currentLength > _maxLength;
                }
                widget.onTextChanged?.call(text);
              });
            },
            onEditingComplete: () {
              widget.editingCompleteCallBack?.call();
            },
            style: gtTextStyleW500(
              fontSize: 14,
              color: GTColorsV3.text1.color,
            ),
          ),
        ),
        if ((widget.maxLength ?? 0) > 0) ...[
          const SizedBox(
            height: 8,
          ),
          Align(
            alignment: Alignment.bottomRight,
            child: Text(
              "$currentLength/${widget.maxLength ?? 0}",
              style: gtTextStyleW400(color: isError ? GTColorsV3.funct3.color : GTColorsV3.text3.color, fontSize: 12),
            ),
          ),
        ],
      ],
    );
  }
}
